package cn.liccshop.sys.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

/**
 * @program: LiCCShop
 * @description: Mybatis-Plus 代码生成器配置类
 * @author: LiCC
 * @data 2019-03-05-21:32
 */
public class MpGenerator {
    /**
     * 自定义模板路径
     */
    private final static String MY_TEMPLATE_URL = "D:\\CodeWork\\JavaProject\\liccshop\\src\\main\\resources\\templates\\";
    private final static String CLIENT_BACKSTAGE = "backstage.";
    private final static String CLIENT_PROSCENIUM = "proscenium.";
    private final static String SERVICE = "service.";
    /**
     * 选择平台 前台后台包路径
     */
    private final static String CLIENT_CHOICE = SERVICE;
    private final static String ENTITY_NAME = CLIENT_CHOICE + "shopTransaction--------*";
    /**
     * 数据库表名称
     */
    private final static String DB_NAME = "shop_transaction--------*";

    public static void main(String[] args) {
        /**
         * 代码生成器
         */
        AutoGenerator mpg = new AutoGenerator();
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        /**
         * 全局配置
         */
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor("LiCC")
                .setOutputDir("D:\\CodeWork\\JavaProject\\liccshop\\src\\main\\java")
                .setFileOverride(true)
                .setEnableCache(false)
                .setIdType(IdType.UUID);
        mpg.setGlobalConfig(globalConfig);
        /**
         * 数据源配置
         */
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl("jdbc:mysql://localhost:3306/licc_shop?tinyInt1isBit=false&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT")
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUsername("root")
                .setPassword("123456");
        mpg.setDataSource(dataSourceConfig);
        /**
         * 包配置
         */
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(null)
                .setParent("cn.liccshop.core")
                .setEntity("entity")
                .setXml(ENTITY_NAME + ".mapper")
                .setMapper(ENTITY_NAME + ".mapper")
                .setService(ENTITY_NAME + ".service")
                .setServiceImpl(ENTITY_NAME + ".service")
                .setController(ENTITY_NAME + ".controller");
        mpg.setPackageInfo(packageConfig);
        /**
         * 策略配置
         */
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setInclude(DB_NAME)
                .setRestControllerStyle(true)
                .setEntityTableFieldAnnotationEnable(true)
                .setEntityBooleanColumnRemoveIsPrefix(true)
                .setEntityLombokModel(true)
                .setSuperEntityClass("cn.liccshop.sys.base.BaseBean")
                .setSuperControllerClass("cn.liccshop.sys.base.MyActionController");
        mpg.setStrategy(strategyConfig);
        /**
         * 自定义模板
         */
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("/templates/entity.java")
                .setController("/templates/controller_" + CLIENT_PROSCENIUM + "java");
        mpg.setTemplate(templateConfig);
        /**
         * 执行生成
         */
        mpg.execute();
    }
}

